package com.livesource.client.webservices.github;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.googlecode.gwt.crypto.bouncycastle.util.encoders.Base64;
import com.livesource.client.LiveSourceGlobalVariables;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.model.project.Project;
import com.livesource.client.utilities.ConvertJson;
import com.livesource.client.utilities.EncryptText;
import com.livesource.client.utilities.GetCookies;

public class RequestGithubFileContent {

	// {"encoding":"base64","sha":"4f1e1d08b0c0e2de3b2d5f9da0175f710d88a52d",
	// "content":"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxjbGFz\nc3BhdGg+DQoJPGNsYXNzcGF0aGVudHJ5IGtpbmQ9InNyYyIgcGF0aD0ic3Jj\nIi8+DQoJPGNsYXNzcGF0aGVudHJ5IGtpbmQ9ImNvbiIgcGF0aD0ib3JnLmVj\nbGlwc2UuamR0LmxhdW5jaGluZy5KUkVfQ09OVEFJTkVSL29yZy5lY2xpcHNl\nLmpkdC5pbnRlcm5hbC5kZWJ1Zy51aS5sYXVuY2hlci5TdGFuZGFyZFZNVHlw\nZS9qcmU2Ij4NCgkJPGF0dHJpYnV0ZXM+DQoJCQk8YXR0cmlidXRlIG5hbWU9\nIm93bmVyLnByb2plY3QuZmFjZXRzIiB2YWx1ZT0iamF2YSIvPg0KCQk8L2F0\ndHJpYnV0ZXM+DQoJPC9jbGFzc3BhdGhlbnRyeT4NCgk8Y2xhc3NwYXRoZW50\ncnkga2luZD0iY29uIiBwYXRoPSJvcmcuZWNsaXBzZS5qc3Quc2VydmVyLmNv\ncmUuY29udGFpbmVyL29yZy5lY2xpcHNlLmpzdC5zZXJ2ZXIuY29yZS5ydW50\naW1lVGFyZ2V0L2NvbS5hbWF6b25hd3MuZWNsaXBzZS5lbGFzdGljYmVhbnN0\nYWxrLmplZS5ydW50aW1lIj4NCgkJPGF0dHJpYnV0ZXM+DQoJCQk8YXR0cmli\ndXRlIG5hbWU9Im93bmVyLnByb2plY3QuZmFjZXRzIiB2YWx1ZT0ianN0Lndl\nYiIvPg0KCQk8L2F0dHJpYnV0ZXM+DQoJPC9jbGFzc3BhdGhlbnRyeT4NCgk8\nY2xhc3NwYXRoZW50cnkga2luZD0iY29uIiBwYXRoPSJvcmcuZWNsaXBzZS5q\nc3QuajJlZS5pbnRlcm5hbC53ZWIuY29udGFpbmVyIi8+DQoJPGNsYXNzcGF0\naGVudHJ5IGtpbmQ9ImNvbiIgcGF0aD0ib3JnLmVjbGlwc2UuanN0LmoyZWUu\naW50ZXJuYWwubW9kdWxlLmNvbnRhaW5lciIvPg0KCTxjbGFzc3BhdGhlbnRy\neSBraW5kPSJjb24iIHBhdGg9ImNvbS5hbWF6b25hd3MuZWNsaXBzZS5zZGsu\nQVdTX0pBVkFfU0RLIj4NCgkJPGF0dHJpYnV0ZXM+DQoJCQk8YXR0cmlidXRl\nIG5hbWU9Im9yZy5lY2xpcHNlLmpzdC5jb21wb25lbnQuZGVwZW5kZW5jeSIg\ndmFsdWU9Ii9XRUItSU5GL2xpYiIvPg0KCQk8L2F0dHJpYnV0ZXM+DQoJPC9j\nbGFzc3BhdGhlbnRyeT4NCgk8Y2xhc3NwYXRoZW50cnkga2luZD0ib3V0cHV0\nIiBwYXRoPSJidWlsZC9jbGFzc2VzIi8+DQo8L2NsYXNzcGF0aD4NCg==\n",
	// "size":1075,"url":"https://api.github.com/repos/allineo/TravelLog/git/blobs/4f1e1d08b0c0e2de3b2d5f9da0175f710d88a52d"}

	public static void getJsonpRequest(Project project, final ProjectClass file) {

		String authenticationToken = EncryptText.decrypt(GetCookies
				.getGithubAuthenticationToken());

		final GithubAPIServiceAsync githubAPIService = GWT
				.create(GithubAPIService.class);

		if (!(authenticationToken == null || "".equals(authenticationToken))) {

			githubAPIService.githubGetFile(authenticationToken,
					project.getRepositoryURL(), file.getFileExtension(),
					file.getSha(), new AsyncCallback<String>() {

						public void onFailure(final Throwable caught) {
							System.out.println(caught);
						}

						public void onSuccess(final String jsonResult) {

							if (jsonResult != null) {

								JSONObject obj = (JSONObject) JSONParser
										.parseStrict(jsonResult);

								ProjectClass projectClass = LiveSourceGlobalVariables.projectEntries
										.get(file.toString());

								String fileContentEncoded = ConvertJson
										.convertToString(obj.get("fileContent"));

								projectClass
										.setFileContent(decodeBase64(fileContentEncoded));

								projectClass.setFeatureDescription(ConvertJson
										.convertToString(obj
												.get("classDescription")));

								projectClass.setFeatureType(ConvertJson
										.convertToString(obj.get("classType")));

								projectClass.setStatus(ConvertJson
										.convertToString(obj.get("classStatus")));

								LiveSourceGlobalVariables.projectEntries.put(
										projectClass.toString(), projectClass);
							}
						}
					});
		}
	}

	public static String decodeBase64(String encodedString) {

		byte[] decodedStr = Base64.decode(encodedString);

		return new String(decodedStr);
	}
}
